package cn.clx.ymcc.service.impl;

import cn.clx.ymcc.constants.YmccExceptionError;
import cn.clx.ymcc.domain.MessageOver;
import cn.clx.ymcc.domain.MessageSms;
import cn.clx.ymcc.mapper.MessageOverMapper;
import cn.clx.ymcc.service.IMessageOverService;
import cn.clx.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author clx
 * @since 2022-07-18
 */
@Service
public class MessageOverServiceImpl extends ServiceImpl<MessageOverMapper, MessageOver> implements IMessageOverService {

    @Autowired
    private MessageOverMapper messageOverMapper;

    @Override
    public void getout(MessageSms sms) {
        //首先判断是否已经在黑名单中  只做了电话的拉黑  ip拉黑 我的整个ip都不能登录了
        Wrapper<MessageOver> tWrapper = new EntityWrapper();
        Wrapper<MessageOver> message = tWrapper.eq("phone", sms.getPhone());// where phone = 123123
        Object o = selectObj(message);
        AssertUtil.isNull(o,YmccExceptionError.MESSAGESMS_IS_EXIST.getMessage());

        //这是ip的拉黑
      /*  Wrapper<MessageOver> tWrapper1 = new EntityWrapper();
        Wrapper<MessageOver> message1 = tWrapper.eq("ip", sms.getIp());// where phone = 123123
        Object o1 = selectObj(message1);
        AssertUtil.isNull(o1,YmccExceptionError.MESSAGESMS_IS_EXIST.getMessage());*/

        MessageOver messageOver = new MessageOver();
        messageOver.setIp(sms.getIp());
        messageOver.setUserId(sms.getUserId());
        messageOver.setCreatetime(new Date());
        messageOver.setPhone(sms.getPhone());
        messageOver.setTitle(sms.getNote());
        insert(messageOver);
    }

    @Override
    public MessageOver loadByPhone(String phone) {
        return messageOverMapper.loadByPhone(phone);
    }

    /**
     * 移出黑名单
     * @param sms
     */
    @Override
    public void removeBlack(MessageSms sms) {
        Wrapper<MessageOver> tWrapper = new EntityWrapper();
        Wrapper<MessageOver> message = tWrapper.eq("phone", sms.getPhone());// where phone = 123123
        boolean delete = delete(message);
        AssertUtil.isTrue(delete,YmccExceptionError.MESSAGESMS_REMOVE_FAILE.getMessage());
    }
}
